﻿using Ops.Mpc.States;
using Ops.Mpc.ExampleApplication.ExampleService;
using Ops.Mpc.Events;
using Ops.Mpc.ExampleApplication.DataTemplateBindings.Models;
using System.Linq;
using Ops.Mpc.Collections;
using Ops.Mpc.ExampleApplication.Models;
using Ops.Mpc.Ioc;


namespace Ops.Mpc.ExampleApplication.DataTemplateBindings.Actions
{
    public class GetPersonsStateAction : StateAction
    {
        #region Fields
        private DataTemplateModel _dataTemplateModel = null;
        #endregion 

        public override void Start()
        {
            base.Start();
            _dataTemplateModel = this.GetModel<DataTemplateModel>();
        }

        protected override void OnExecute(object context)
        {
            base.OnExecute(context);

            this.SetBusyMessage( "Getting page 1" );

            IocServiceLocator.Get<IExampleServiceContext>().GetPersons(new Pager() { CurrentPageIndex = 0, PageSize = 10 }, GetPersonsCompleted, OnError);
        }

        private void GetPersonsCompleted(Person[] persons, object context)
        {

            this.SetBusyMessage( "Loading page 1" );

            _dataTemplateModel.Persons.Clear();

            foreach (Person person in persons)
                _dataTemplateModel.Persons.Add(person);


            this.Finish();
        }
    }
}
